﻿namespace WMS6.Web.Outbound
{
    using System;
    using System.Collections.Generic;
    using System.Configuration;
    using System.Data;
    using System.Runtime.InteropServices;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using WMS6.Web;
    using WMS6.Web.InventoryMgtServiceRef;
    using WMS6.Web.OutboundServiceRef;
    using WMS6.Web.UserControls;

    public class ManualAllocation : BasePage
    {
        protected Button btnGoBack;
        protected Button btnPutAway;
        protected Button btnSave;
        protected Button btnSearch;
        protected HtmlForm form1;
        protected GridView gvAllocation;
        protected GridView gvInvLotLocLpn;
        protected HtmlInputHidden hdnAllocatedQty;
        protected HtmlInputHidden hdnCurOption;
        protected HtmlInputHidden hdnOriginQty;
        protected HtmlInputHidden hdnQty;
        protected HtmlInputHidden hdnRemainQty;
        protected HtmlInputHidden hdnRtnValue;
        protected HtmlHead Head1;
        protected Label lblAllocatedQty;
        protected Label lblExpireDate;
        protected Label lblExternalLot;
        protected Label lblExternalReceiptID;
        protected Label lblFormatExample;
        protected Label lblLocID;
        protected Label lblLotAttr01;
        protected Label lblLotAttr02;
        protected Label lblLotAttr03;
        protected Label lblLotAttr04;
        protected Label lblLotAttr05;
        protected Label lblLotAttr06;
        protected Label lblLotAttr07;
        protected Label lblLotAttr08;
        protected Label lblLotAttr09;
        protected Label lblLotAttr10;
        protected Label lblManualPA;
        protected Label lblMsg;
        protected Label lblOrderQty;
        protected Label lblPendingAllocQty;
        protected Label lblProduceDate;
        protected Label lblQty;
        protected Label lblReceiptDate;
        protected Label lblReceiptID;
        protected Label lblSearchLocID;
        protected Label lblSkuID;
        protected Label lblSkuID2;
        protected Label lblUomID;
        protected Label lblVendorID;
        protected HtmlLink mycss;
        protected Label one1;
        protected Label one2;
        protected Label one3;
        protected Pager PagerFooter;
        protected ScriptManager ScriptManager1;
        protected Label two1;
        protected Label two2;
        protected TextBox txtAllocatedQty;
        protected TextBox txtExpireDate;
        protected TextBox txtExternalLot;
        protected TextBox txtExternalReceiptID;
        protected TextBox txtLocID;
        protected TextBox txtLotAttr01;
        protected TextBox txtLotAttr02;
        protected TextBox txtLotAttr03;
        protected TextBox txtLotAttr04;
        protected TextBox txtLotAttr05;
        protected TextBox txtLotAttr06;
        protected TextBox txtLotAttr07;
        protected TextBox txtLotAttr08;
        protected TextBox txtLotAttr09;
        protected TextBox txtLotAttr10;
        protected TextBox txtOrderQty;
        protected TextBox txtPendingAllocQty;
        protected TextBox txtProduceDate;
        protected TextBox txtQty;
        protected TextBox txtReceiptDate;
        protected TextBox txtReceiptID;
        protected TextBox txtSearchLocID;
        protected TextBox txtSkuID;
        protected TextBox txtSkuID2;
        protected TextBox txtVendorID;

        private List<PickDetailInfo> AllocatedByInventory(out decimal allocatedQty)
        {
            OrderDetailInfo orderDetailEntity = this.Session["Allocation"] as OrderDetailInfo;
            List<PickDetailInfo> pickDetailInfoList = new List<PickDetailInfo>();
            PickDetailInfo pickDetaiInfo = null;
            allocatedQty = 0M;
            for (int i = 0; i < this.gvInvLotLocLpn.Rows.Count; i++)
            {
                if (!string.IsNullOrEmpty(((TextBox) this.gvInvLotLocLpn.Rows[i].FindControl("txtAllocatedQty")).Text.Trim()) && (int.Parse(((TextBox) this.gvInvLotLocLpn.Rows[i].FindControl("txtAllocatedQty")).Text.Trim()) > 0))
                {
                    pickDetaiInfo = new PickDetailInfo {
                        AltSkuID = orderDetailEntity.AltSkuID,
                        CartonGroup = orderDetailEntity.CartonGroup
                    };
                    DateTime CS00002 = new DateTime();
                    if (pickDetaiInfo.EffectiveDate != CS00002)
                    {
                        pickDetaiInfo.EffectiveDate = pickDetaiInfo.EffectiveDate;
                    }
                    else
                    {
                        pickDetaiInfo.EffectiveDate = DateTime.Now;
                    }
                    pickDetaiInfo.Lot = this.gvInvLotLocLpn.DataKeys[i]["LOT"].ToString();
                    pickDetaiInfo.Loc = this.gvInvLotLocLpn.DataKeys[i]["LOC"].ToString();
                    pickDetaiInfo.LpnID = this.gvInvLotLocLpn.DataKeys[i]["LPN_ID"].ToString();
                    pickDetaiInfo.OrderID = orderDetailEntity.OrderID;
                    pickDetaiInfo.OrderLineID = orderDetailEntity.LineID;
                    pickDetaiInfo.OwnerID = orderDetailEntity.OwnerID;
                    pickDetaiInfo.PackID = orderDetailEntity.PackID;
                    pickDetaiInfo.Qty = decimal.Parse(((TextBox) this.gvInvLotLocLpn.Rows[i].FindControl("txtAllocatedQty")).Text.Trim());
                    pickDetaiInfo.SkuID = orderDetailEntity.SkuID;
                    pickDetaiInfo.Status = "New";
                    pickDetaiInfo.UomID = orderDetailEntity.UomID;
                    pickDetaiInfo.CaseID = "";
                    pickDetaiInfo.AltSkuID = "";
                    pickDetaiInfo.DropID = "";
                    pickDetaiInfo.UomQty = 0M;
                    pickDetaiInfo.MovedQty = 0M;
                    pickDetaiInfo.UpdateSource = "";
                    pickDetaiInfo.PickMethod = "";
                    pickDetaiInfo.WaveID = "";
                    pickDetaiInfo.FreightCharges = 0.0;
                    pickDetaiInfo.IsClosed = "";
                    pickDetaiInfo.QcStatus = "";
                    pickDetaiInfo.CreatedBy = base.UserInfo.UserID;
                    pickDetaiInfo.UpdatedBy = base.UserInfo.UserID;
                    allocatedQty += pickDetaiInfo.Qty;
                    pickDetailInfoList.Add(pickDetaiInfo);
                }
            }
            return pickDetailInfoList;
        }

        private void BindDataInvLocLotLpnList(InvLotLocLpnQueryEntity InvLtLocLpnEntity)
        {
            this.PagerFooter.CurrentPage = int.Parse(InvLtLocLpnEntity.CurrentPage);
            DataSet ds = new DataSet();
            ds = InventoryMgtServiceClientProxy.GetInstance().GetInvLotLocLpnByQueryEntity(InvLtLocLpnEntity);
            base.BindData2GridView(this.gvInvLotLocLpn, ds);
            if ((ds != null) && (ds.Tables.Count > 1))
            {
                this.PagerFooter.PageInfo = ds.Tables[1].Rows[0]["CurrentPage"].ToString() + " / " + ds.Tables[1].Rows[0]["TotalPagesCount"].ToString() + "&nbsp;&nbsp;&nbsp;共: " + ds.Tables[1].Rows[0]["TotalRecordsCount"].ToString();
                this.PagerFooter.TotalPages = int.Parse(ds.Tables[1].Rows[0]["TotalPagesCount"].ToString());
            }
        }

        protected void btnFirst_Click(object sender, EventArgs e)
        {
            InvLotLocLpnQueryEntity invLotLocLpnEntity = this.GetInvLotLocLpnQueryEntity();
            invLotLocLpnEntity.CurrentPage = "0";
            this.BindDataInvLocLotLpnList(invLotLocLpnEntity);
            ScriptManager.RegisterStartupScript(this.Page, base.GetType(), "", "<script language='javascript'>setTab2('two',2,2);</script>", false);
        }

        protected void btnGoBack_Click(object sender, EventArgs e)
        {
            this.hdnRtnValue.Value = "2,0";
        }

        protected void btnLast_Click(object sender, EventArgs e)
        {
            InvLotLocLpnQueryEntity invLotLocLpnEntity = this.GetInvLotLocLpnQueryEntity();
            invLotLocLpnEntity.CurrentPage = (this.PagerFooter.TotalPages - 1).ToString();
            this.BindDataInvLocLotLpnList(invLotLocLpnEntity);
            ScriptManager.RegisterStartupScript(this.Page, base.GetType(), "", "<script language='javascript'>setTab2('two',2,2);</script>", false);
        }

        protected void btnNext_Click(object sender, EventArgs e)
        {
            InvLotLocLpnQueryEntity invLotLocLpnEntity = this.GetInvLotLocLpnQueryEntity();
            if (int.Parse(invLotLocLpnEntity.CurrentPage) < (this.PagerFooter.TotalPages - 1))
            {
                invLotLocLpnEntity.CurrentPage = (int.Parse(invLotLocLpnEntity.CurrentPage) + 1).ToString();
                this.BindDataInvLocLotLpnList(invLotLocLpnEntity);
            }
            ScriptManager.RegisterStartupScript(this.Page, base.GetType(), "", "<script language='javascript'>setTab2('two',2,2);</script>", false);
        }

        protected void btnPrevious_Click(object sender, EventArgs e)
        {
            InvLotLocLpnQueryEntity invLotLocLpnEntity = this.GetInvLotLocLpnQueryEntity();
            if (int.Parse(invLotLocLpnEntity.CurrentPage) > 0)
            {
                invLotLocLpnEntity.CurrentPage = (int.Parse(invLotLocLpnEntity.CurrentPage) - 1).ToString();
                this.BindDataInvLocLotLpnList(invLotLocLpnEntity);
            }
            ScriptManager.RegisterStartupScript(this.Page, base.GetType(), "", "<script language='javascript'>setTab2('two',2,2);</script>", false);
        }

        protected void btnPutAway_Click(object sender, EventArgs e)
        {
            try
            {
                if (string.IsNullOrEmpty(this.txtLocID.Text.Trim()))
                {
                    this.lblMsg.Text = "请输入货位!";
                    this.txtLocID.Focus();
                }
                else
                {
                    List<PickDetailInfo> pickDetailInfoList = null;
                    if (this.Session["ManualAllocation"] != null)
                    {
                        pickDetailInfoList = this.Session["ManualAllocation"] as List<PickDetailInfo>;
                    }
                    else
                    {
                        pickDetailInfoList = new List<PickDetailInfo>();
                    }
                    OrderDetailInfo orderDetailEntity = this.Session["Allocation"] as OrderDetailInfo;
                    ManualAllocateParams manualAllocateParams = new ManualAllocateParams {
                        SkuID = orderDetailEntity.SkuID,
                        OwnerID = orderDetailEntity.OwnerID,
                        Loc = this.txtLocID.Text.Trim(),
                        Lpn = orderDetailEntity.LpnID,
                        Qty = decimal.Parse(this.txtQty.Text),
                        PackID = orderDetailEntity.PackID,
                        UomID = orderDetailEntity.UomID,
                        LotAttr01 = orderDetailEntity.LotAttr01,
                        LotAttr02 = orderDetailEntity.LotAttr02,
                        LotAttr03 = orderDetailEntity.LotAttr03,
                        LotAttr04 = orderDetailEntity.LotAttr04,
                        LotAttr05 = orderDetailEntity.LotAttr05,
                        LotAttr06 = orderDetailEntity.LotAttr06,
                        LotAttr07 = orderDetailEntity.LotAttr07,
                        LotAttr08 = orderDetailEntity.LotAttr08,
                        LotAttr09 = orderDetailEntity.LotAttr09,
                        LotAttr10 = orderDetailEntity.LotAttr10,
                        ProduceDate = orderDetailEntity.ProduceDate,
                        ExpiryDate = orderDetailEntity.ExpiryDate,
                        ExternalLot = orderDetailEntity.ExternalLot,
                        VendorID = orderDetailEntity.VendorID
                    };
                    List<WMS6.Web.OutboundServiceRef.InvLotLocLpnInfo> invLotLocLpnInfo = OutboundServiceClientProxy.GetInstance().CheckLocationQty4Allocation(manualAllocateParams);
                    if ((invLotLocLpnInfo != null) && (invLotLocLpnInfo.Count > 0))
                    {
                        PickDetailInfo pickDetaiInfo = null;
                        int decqty = 0;
                        foreach (WMS6.Web.OutboundServiceRef.InvLotLocLpnInfo info in invLotLocLpnInfo)
                        {
                            pickDetaiInfo = new PickDetailInfo {
                                AltSkuID = orderDetailEntity.AltSkuID,
                                CartonGroup = orderDetailEntity.CartonGroup,
                                CreatedBy = base.UserInfo.UserID
                            };
                            DateTime CS00002 = new DateTime();
                            if (pickDetaiInfo.EffectiveDate != CS00002)
                            {
                                pickDetaiInfo.EffectiveDate = pickDetaiInfo.EffectiveDate;
                            }
                            else
                            {
                                pickDetaiInfo.EffectiveDate = DateTime.Now;
                            }
                            pickDetaiInfo.Lot = info.Lot;
                            pickDetaiInfo.Loc = info.Loc;
                            pickDetaiInfo.LpnID = info.LpnID;
                            pickDetaiInfo.OrderID = orderDetailEntity.OrderID;
                            pickDetaiInfo.OrderLineID = orderDetailEntity.LineID;
                            pickDetaiInfo.OwnerID = info.OwnerID;
                            pickDetaiInfo.PackID = orderDetailEntity.PackID;
                            pickDetaiInfo.Qty = info.Qty;
                            decqty += Convert.ToInt32(info.Qty);
                            pickDetaiInfo.SkuID = info.SkuID;
                            pickDetaiInfo.Status = "New";
                            pickDetaiInfo.UomID = orderDetailEntity.UomID;
                            pickDetaiInfo.CaseID = "";
                            pickDetaiInfo.AltSkuID = "";
                            pickDetaiInfo.DropID = "";
                            pickDetaiInfo.UomQty = 0M;
                            pickDetaiInfo.MovedQty = 0M;
                            pickDetaiInfo.UpdateSource = "";
                            pickDetaiInfo.PickMethod = "";
                            pickDetaiInfo.WaveID = "";
                            pickDetaiInfo.FreightCharges = 0.0;
                            pickDetaiInfo.IsClosed = "";
                            pickDetaiInfo.QcStatus = "";
                            pickDetaiInfo.CreatedBy = base.UserInfo.UserID;
                            pickDetaiInfo.UpdatedBy = pickDetaiInfo.CreatedBy;
                            pickDetailInfoList.Add(pickDetaiInfo);
                        }
                        this.Session["ManualAllocation"] = pickDetailInfoList;
                        int remainQty = int.Parse(this.hdnQty.Value) - decqty;
                        this.txtQty.Text = remainQty.ToString();
                        this.hdnQty.Value = remainQty.ToString();
                        base.BindData2GridView(this.gvAllocation, pickDetailInfoList);
                    }
                    else
                    {
                        this.lblMsg.Text = "此货位上没有找到合适批次的货品记录!";
                        this.lblMsg.Visible = true;
                    }
                }
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
                this.lblMsg.Visible = true;
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                List<PickDetailInfo> pickDetailInfoList;
                if (this.hdnCurOption.Value == "1")
                {
                    if (!string.IsNullOrEmpty(this.hdnQty.Value) && (int.Parse(this.hdnQty.Value) == 0))
                    {
                        if (this.Session["ManualAllocation"] != null)
                        {
                            pickDetailInfoList = this.Session["ManualAllocation"] as List<PickDetailInfo>;
                            if ((pickDetailInfoList != null) && (pickDetailInfoList.Count > 0))
                            {
                                OutboundServiceClientProxy.GetInstance().ManualAllocate(pickDetailInfoList);
                                this.Session["pickDetailInfoList"] = null;
                                this.hdnRtnValue.Value = "2";
                            }
                        }
                    }
                    else
                    {
                        this.lblMsg.Text = "请将剩余货品进行分配!";
                        this.lblMsg.Visible = true;
                        this.txtQty.Text = Convert.ToInt32(this.hdnQty.Value).ToString();
                    }
                }
                else
                {
                    decimal allocatedQty = 0M;
                    pickDetailInfoList = this.AllocatedByInventory(out allocatedQty);
                    OutboundServiceClientProxy.GetInstance().ManualAllocate(pickDetailInfoList);
                    this.hdnRtnValue.Value = "2";
                }
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
                this.lblMsg.Visible = true;
            }
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            OrderDetailInfo orderDetailEntity = this.Session["Allocation"] as OrderDetailInfo;
            InvLotLocLpnQueryEntity InvLtLocLpnEntity = new InvLotLocLpnQueryEntity {
                IsGetAll = false,
                CurrentPage = "0",
                PageSize = string.IsNullOrEmpty(ConfigurationManager.AppSettings["MANUAL_ALLOCATE_GV_DETAIL_COUNT"]) ? "20" : ConfigurationManager.AppSettings["MANUAL_ALLOCATE_GV_DETAIL_COUNT"].ToString(),
                SortDirection = "ASC",
                SortField = "LOC",
                OwnerID = base.GetParamsString(orderDetailEntity.OwnerID, "Same As"),
                SkuID = base.GetParamsString(orderDetailEntity.SkuID, "Same As"),
                Status = base.GetParamsString("OK", "Same As")
            };
            if (!string.IsNullOrEmpty(this.txtSearchLocID.Text.Trim()))
            {
                InvLtLocLpnEntity.Loc = base.GetParamsString(this.txtSearchLocID.Text.Trim(), "Contains");
            }
            if (!string.IsNullOrEmpty(this.txtReceiptID.Text.Trim()))
            {
                InvLtLocLpnEntity.ReceiptID = base.GetParamsString(this.txtReceiptID.Text.Trim(), "Contains");
            }
            if (!string.IsNullOrEmpty(this.txtExternalReceiptID.Text.Trim()))
            {
                InvLtLocLpnEntity.ExternalReceiptID = base.GetParamsString(this.txtExternalReceiptID.Text.Trim(), "Contains");
            }
            if (!string.IsNullOrEmpty(this.txtExternalLot.Text.Trim()))
            {
                InvLtLocLpnEntity.ExternalLot = base.GetParamsString(this.txtExternalLot.Text.Trim(), "Contains");
            }
            if (!string.IsNullOrEmpty(this.txtProduceDate.Text.Trim()))
            {
                InvLtLocLpnEntity.ProduceDate = base.GetParamsString(this.txtProduceDate.Text.Trim(), "Contains");
            }
            if (!string.IsNullOrEmpty(this.txtExpireDate.Text.Trim()))
            {
                InvLtLocLpnEntity.ExpiryDate = base.GetParamsString(this.txtExpireDate.Text.Trim(), "Contains");
            }
            if (!string.IsNullOrEmpty(this.txtReceiptDate.Text.Trim()))
            {
                InvLtLocLpnEntity.ReceiptDate = base.GetParamsString(this.txtReceiptDate.Text.Trim(), "Contains");
            }
            if (!string.IsNullOrEmpty(this.txtVendorID.Text.Trim()))
            {
                InvLtLocLpnEntity.VendorID = base.GetParamsString(this.txtVendorID.Text.Trim(), "Contains");
            }
            if (!string.IsNullOrEmpty(this.txtLotAttr01.Text.Trim()))
            {
                InvLtLocLpnEntity.LotAttr01 = base.GetParamsString(this.txtLotAttr01.Text.Trim(), "Contains");
            }
            if (!string.IsNullOrEmpty(this.txtLotAttr02.Text.Trim()))
            {
                InvLtLocLpnEntity.LotAttr02 = base.GetParamsString(this.txtLotAttr02.Text.Trim(), "Contains");
            }
            if (!string.IsNullOrEmpty(this.txtLotAttr03.Text.Trim()))
            {
                InvLtLocLpnEntity.LotAttr03 = base.GetParamsString(this.txtLotAttr03.Text.Trim(), "Contains");
            }
            if (!string.IsNullOrEmpty(this.txtLotAttr04.Text.Trim()))
            {
                InvLtLocLpnEntity.LotAttr04 = base.GetParamsString(this.txtLotAttr04.Text.Trim(), "Contains");
            }
            if (!string.IsNullOrEmpty(this.txtLotAttr05.Text.Trim()))
            {
                InvLtLocLpnEntity.LotAttr05 = base.GetParamsString(this.txtLotAttr05.Text.Trim(), "Contains");
            }
            if (!string.IsNullOrEmpty(this.txtLotAttr06.Text.Trim()))
            {
                InvLtLocLpnEntity.LotAttr06 = base.GetParamsString(this.txtLotAttr06.Text.Trim(), "Contains");
            }
            if (!string.IsNullOrEmpty(this.txtLotAttr07.Text.Trim()))
            {
                InvLtLocLpnEntity.LotAttr07 = base.GetParamsString(this.txtLotAttr07.Text.Trim(), "Contains");
            }
            if (!string.IsNullOrEmpty(this.txtLotAttr08.Text.Trim()))
            {
                InvLtLocLpnEntity.LotAttr08 = base.GetParamsString(this.txtLotAttr08.Text.Trim(), "Contains");
            }
            if (!string.IsNullOrEmpty(this.txtLotAttr09.Text.Trim()))
            {
                InvLtLocLpnEntity.LotAttr09 = base.GetParamsString(this.txtLotAttr09.Text.Trim(), "Contains");
            }
            if (!string.IsNullOrEmpty(this.txtLotAttr10.Text.Trim()))
            {
                InvLtLocLpnEntity.LotAttr10 = base.GetParamsString(this.txtLotAttr10.Text.Trim(), "Contains");
            }
            InvLtLocLpnEntity.Status = base.GetParamsString("HOLD", "Not Same As");
            InvLtLocLpnEntity.Qty = ">0";
            this.SetInvLotLocLpnQueryEntity(InvLtLocLpnEntity);
            this.BindDataInvLocLotLpnList(InvLtLocLpnEntity);
            ScriptManager.RegisterStartupScript(this.Page, base.GetType(), "", "<script language='javascript'>setTab2('two',2,2);</script>", false);
        }

        private InvLotLocLpnQueryEntity GetInvLotLocLpnQueryEntity()
        {
            return (base.PageQueryEntityViewState as InvLotLocLpnQueryEntity);
        }

        protected void GoTo_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(((TextBox) this.PagerFooter.FindControl("txtGo2Page")).Text.Trim()))
            {
                InvLotLocLpnQueryEntity invLotLocLpnEntity = this.GetInvLotLocLpnQueryEntity();
                invLotLocLpnEntity.CurrentPage = this.PagerFooter.GoToPage.ToString();
                this.BindDataInvLocLotLpnList(invLotLocLpnEntity);
            }
            ScriptManager.RegisterStartupScript(this.Page, base.GetType(), "", "<script language='javascript'>setTab2('two',2,2);</script>", false);
        }

        protected void gvAllocation_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            this.gvAllocation.PageIndex = e.NewPageIndex;
            List<PickDetailInfo> pickDetailInfoList = this.Session["ManualAllocation"] as List<PickDetailInfo>;
            base.BindData2GridView(this.gvAllocation, pickDetailInfoList);
        }

        protected void gvAllocation_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Cells[1].Text = (e.Row.RowIndex + 1).ToString();
            }
        }

        protected void gvAllocation_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            List<PickDetailInfo> pickDetailInfo = null;
            if (this.Session["ManualAllocation"] != null)
            {
                pickDetailInfo = this.Session["ManualAllocation"] as List<PickDetailInfo>;
                decimal remainQty = Convert.ToDecimal(this.hdnQty.Value) + pickDetailInfo[e.RowIndex].Qty;
                this.txtQty.Text = Convert.ToInt32(remainQty).ToString();
                this.hdnQty.Value = Convert.ToInt32(remainQty).ToString();
                pickDetailInfo.RemoveAt(e.RowIndex);
                this.Session["ManualAllocation"] = pickDetailInfo;
                base.BindData2GridView(this.gvAllocation, pickDetailInfo);
            }
        }

        protected void gvInvLotLocLpn_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Cells[0].Text = (((this.PagerFooter.CurrentPage * this.PagerFooter.PageSize) + e.Row.RowIndex) + 1).ToString();
                e.Row.Attributes.Add("onmouseout", "GridViewRowMouseout(this);");
                e.Row.Attributes.Add("onmouseover", "GridViewRowMouseover(this);");
                if (string.IsNullOrEmpty(((Label) e.Row.FindControl("lblAvailableQTY")).Text) || (Convert.ToDecimal(((Label) e.Row.FindControl("lblAvailableQTY")).Text) == 0M))
                {
                    ((TextBox) e.Row.FindControl("txtAllocatedQty")).Enabled = false;
                }
            }
        }

        protected void gvInvLotLocLpn_Sorting(object sender, GridViewSortEventArgs e)
        {
            try
            {
                InvLotLocLpnQueryEntity invLotLocLpnEntity = this.GetInvLotLocLpnQueryEntity();
                invLotLocLpnEntity.SortField = e.SortExpression;
                if (invLotLocLpnEntity.SortDirection == "ASC")
                {
                    invLotLocLpnEntity.SortDirection = "DESC";
                }
                else
                {
                    invLotLocLpnEntity.SortDirection = "ASC";
                }
                this.BindDataInvLocLotLpnList(invLotLocLpnEntity);
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
            }
            finally
            {
                ScriptManager.RegisterStartupScript(this.Page, base.GetType(), "", "<script language='javascript'>setTab2('two',2,2);</script>", false);
            }
        }

        private void InitInvLotQuery(OrderDetailInfo orderDetailEntity)
        {
            if (!string.IsNullOrEmpty(base.GetPreferenceShortDate(orderDetailEntity.ProduceDate)))
            {
                this.txtProduceDate.Text = base.GetPreferenceShortDate(orderDetailEntity.ProduceDate);
            }
            if (!string.IsNullOrEmpty(base.GetPreferenceShortDate(orderDetailEntity.ExpiryDate)))
            {
                this.txtExpireDate.Text = base.GetPreferenceShortDate(orderDetailEntity.ExpiryDate);
            }
            if (!string.IsNullOrEmpty(orderDetailEntity.ExternalLot))
            {
                this.txtExternalLot.Text = orderDetailEntity.ExternalLot;
            }
            if (!string.IsNullOrEmpty(orderDetailEntity.VendorID))
            {
                this.txtVendorID.Text = orderDetailEntity.VendorID;
            }
            if (!string.IsNullOrEmpty(base.GetPreferenceShortDate(orderDetailEntity.ReceiptDate)))
            {
                this.txtReceiptDate.Text = base.GetPreferenceShortDate(orderDetailEntity.ReceiptDate);
            }
            if (!string.IsNullOrEmpty(orderDetailEntity.LotAttr01))
            {
                this.txtLotAttr01.Text = orderDetailEntity.LotAttr01;
            }
            if (!string.IsNullOrEmpty(orderDetailEntity.LotAttr02))
            {
                this.txtLotAttr02.Text = orderDetailEntity.LotAttr02;
            }
            if (!string.IsNullOrEmpty(orderDetailEntity.LotAttr03))
            {
                this.txtLotAttr03.Text = orderDetailEntity.LotAttr03;
            }
            if (!string.IsNullOrEmpty(orderDetailEntity.LotAttr04))
            {
                this.txtLotAttr04.Text = orderDetailEntity.LotAttr04;
            }
            if (!string.IsNullOrEmpty(orderDetailEntity.LotAttr05))
            {
                this.txtLotAttr05.Text = orderDetailEntity.LotAttr05;
            }
            if (!string.IsNullOrEmpty(orderDetailEntity.LotAttr06))
            {
                this.txtLotAttr06.Text = orderDetailEntity.LotAttr06;
            }
            if (!string.IsNullOrEmpty(orderDetailEntity.LotAttr07))
            {
                this.txtLotAttr07.Text = orderDetailEntity.LotAttr07;
            }
            if (!string.IsNullOrEmpty(orderDetailEntity.LotAttr08))
            {
                this.txtLotAttr08.Text = orderDetailEntity.LotAttr08;
            }
            if (!string.IsNullOrEmpty(orderDetailEntity.LotAttr09))
            {
                this.txtLotAttr09.Text = orderDetailEntity.LotAttr09;
            }
            if (!string.IsNullOrEmpty(orderDetailEntity.LotAttr10))
            {
                this.txtLotAttr10.Text = orderDetailEntity.LotAttr10;
            }
        }

        private void LoadData()
        {
            if (this.Session["Allocation"] != null)
            {
                OrderDetailInfo orderDetailEntity = this.Session["Allocation"] as OrderDetailInfo;
                this.txtSkuID.Text = orderDetailEntity.SkuID;
                this.txtQty.Text = Convert.ToInt32((decimal) ((((orderDetailEntity.OriginalQty - orderDetailEntity.AllocatedQty) - orderDetailEntity.PreallocatedQty) - orderDetailEntity.PickedQty) - orderDetailEntity.ShippedQty)).ToString();
                this.lblUomID.Text = orderDetailEntity.UomID;
                this.hdnQty.Value = Convert.ToInt32((decimal) ((((orderDetailEntity.OriginalQty - orderDetailEntity.AllocatedQty) - orderDetailEntity.PreallocatedQty) - orderDetailEntity.PickedQty) - orderDetailEntity.ShippedQty)).ToString();
                this.hdnOriginQty.Value = Convert.ToInt32((decimal) ((((orderDetailEntity.OriginalQty - orderDetailEntity.AllocatedQty) - orderDetailEntity.PreallocatedQty) - orderDetailEntity.PickedQty) - orderDetailEntity.ShippedQty)).ToString();
                this.txtSkuID2.Text = orderDetailEntity.SkuID;
                this.txtOrderQty.Text = Convert.ToInt32(orderDetailEntity.OriginalQty).ToString();
                this.txtAllocatedQty.Text = Convert.ToInt32((decimal) (((orderDetailEntity.AllocatedQty + orderDetailEntity.PickedQty) + orderDetailEntity.ShippedQty) + orderDetailEntity.PreallocatedQty)).ToString();
                this.txtPendingAllocQty.Text = Convert.ToInt32((decimal) ((((orderDetailEntity.OriginalQty - orderDetailEntity.AllocatedQty) - orderDetailEntity.PreallocatedQty) - orderDetailEntity.PickedQty) - orderDetailEntity.ShippedQty)).ToString();
                this.hdnAllocatedQty.Value = orderDetailEntity.AllocatedQty.ToString();
                this.InitInvLotQuery(orderDetailEntity);
            }
            this.Session["ManualAllocation"] = null;
            this.ShowGridViewHeader();
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!base.IsPostBack)
            {
                base.InitPage(this.Page);
                this.LoadData();
            }
            this.lblMsg.Text = "";
            base.Response.Expires = -1;
        }

        private void SetInvLotLocLpnQueryEntity(InvLotLocLpnQueryEntity invLotLocLpnQueryEntity)
        {
            base.PageQueryEntityViewState = invLotLocLpnQueryEntity;
        }

        private void ShowGridViewHeader()
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("Loc");
            dt.Columns.Add("Qty");
            dt.Columns.Add("UomID");
            dt.Columns.Add("LpnID");
            DataRow dr = dt.NewRow();
            dt.Rows.Add(dr);
            base.BindData2GridView(this.gvAllocation, dt);
            this.gvAllocation.Rows[0].Cells[1].Text = "";
            this.gvAllocation.Rows[0].Cells[0].Controls.Clear();
        }
    }
}

